package com.chenfan.magic.resolver;

import com.chenfan.magic.utils.Func;
import com.chenfan.magic.utils.SpringUtil;
import lombok.AllArgsConstructor;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 周夕
 * @date 2020-02-26 14:32
 */
@AllArgsConstructor
public class __ResolverBuilder {

    /**
     * 解析器缓存池
     */
    private static Map<String, __IResolver> resolverPool = new ConcurrentHashMap<>();

    static {
        resolverPool.put(FreemarkerResolver.TYPE_CODE, SpringUtil.getBean(FreemarkerResolver.class));
        resolverPool.put("beetl", SpringUtil.getBean(BeetlResolver.class));
        resolverPool.put("enjoy", SpringUtil.getBean(EnjoyResolver.class));
        resolverPool.put("st4", SpringUtil.getBean(ST4Resolver.class));
        resolverPool.put("thymeleaf", SpringUtil.getBean(ThymeleafResolver.class));
        resolverPool.put("velocity", SpringUtil.getBean(VelocityResolver.class));

    }

    /**
     * 获取解析器
     *
     * @param typeCode 类型代码
     * @return _IResolver
     */
    public static __IResolver getResolver(String typeCode) {
        __IResolver iResolver = resolverPool.get(Func.toStr(typeCode, FreemarkerResolver.TYPE_CODE));
        return iResolver;
    }
}
